home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / DELPHI / SOUNDEX.ZIP / SOUNDEX.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1996-02-06  |  10.7 KB  |  326 lines

  1. { ****************************************************************** }
  2. {                                                                    }
  3. {   Delphi component TSoundex                                        }
  4. {                                                                    }
  5. {   Copyright ⌐ 1995 by Indigo Software                              }
  6. {                                                                    }
  7. { ****************************************************************** }
  8.  
  9. (*---------------------------------------------------------------------|
  10. Description:
  11. The Soundex component uses the Soundex algorithm to determine if two
  12. words sound similar.  Useful in database applications where the
  13. operator may not know the exact spelling of a search string, for
  14. example a last name.
  15.  
  16. Properties:
  17.  
  18. FirstWord/SecondWord: String
  19.       The FirstWord and SecondWord properties define the two words that
  20.       are to be compared.  The SoundAlike and SoundAlikePlus properties
  21.       will state whether the words sound similar, depending on which
  22.       method you choose.
  23.  
  24. SoundexValue: String
  25.       The SoundexValue property is a string consisting of a series of
  26.       numbers that depicts the unique sound of the word specified in
  27.       the FirstWord property.
  28.  
  29.       This value can be stored in a hidden field of a database for
  30.       future searches.  When the operator searches for a given string
  31.       (for example, a last name), it can be converted to a SoundexValue,
  32.       and compared to the values in the hidden field, thereby returning
  33.       all records which match the sound of the search string.
  34.  
  35. SoundAlike: Boolean
  36.       The SoundAlike property states whether the words defined by
  37.       FirstWord and SecondWord sound similar according to the Soundex
  38.       algorithm.
  39.  
  40. SoundexPlusValue: String
  41.       The SoundexPlusValue property is a string consisting of a series
  42.       of numbers that depicts the unique sound of the word specified in
  43.       the FirstWord property.
  44.  
  45.       This value can be stored in a hidden field of a database for future
  46.       searches.  When the operator searches for a given string
  47.       (for example, a last name), it can be converted to a SoundexPlusValue,
  48.       and compared to the values in the hidden field, thereby returning all
  49.       records which match the sound of the search string.
  50.  
  51.       In the Soundex algorithm, words that begin with different letters do
  52.       not sound similar.  Therefore, the words phish and fish, or sell and
  53.       cell, would return different SoundexValues.  Because of this, a new
  54.       algorithm, SoundexPlus, was developed.  This algorithm takes the first
  55.       letter into consideration, and in the above examples, returns true.
  56.  
  57. SoundAlikePlus: Boolean
  58.       The SoundAlikePlus property states whether the words defined by
  59.       FirstWord and SecondWord sound similar according to the SoundexPlus
  60.       algorithm.
  61.  
  62. Methods:
  63.  
  64. Soundex(CheckWord:string):string;
  65.       The Soundex method is a function which returns the SoundexValue
  66.       for the CheckWord.
  67.  
  68. SoundexPlus(CheckWord:string):string;
  69.       The SoundexPlus method is a function which returns the
  70.       SoundexPlusValue for the CheckWord.
  71. |---------------------------------------------------------------------*)
  72. unit Soundex;
  73.  
  74. interface
  75.  
  76. {$IFDEF WIN32}
  77. uses Messages, Windows, SysUtils, Classes, Controls, 
  78.      Forms, Menus, Graphics;
  79. {$ELSE}
  80. uses WinTypes, WinProcs, Messages, SysUtils, Classes, Controls, 
  81.      Forms, Menus, Graphics;
  82. {$ENDIF}
  83.  
  84.  
  85. type
  86.   TSoundex = class(TComponent)
  87.     private
  88.       { Private fields of TSoundex }
  89.         { Storage for property FirstWord }
  90.         FFirstWord : String;
  91.         { Storage for property SecondWord }
  92.         FSecondWord : String;
  93.         { Storage for property SoundexValue }
  94.         FSoundexValue : String;
  95.         { Storage for property SoundAlike }
  96.         FSoundAlike : Boolean;
  97.         { Storage for property SoundexPlusValue }
  98.         FSoundexPlusValue : String;
  99.         { Storage for property SoundAlikePlus }
  100.         FSoundAlikePlus : Boolean;
  101.  
  102.       { Private methods of TSoundex }
  103.         { Method to set variable and property values and create objects }
  104.         procedure AutoInitialize;
  105.         { Method to free any objects created by AutoInitialize }
  106.         procedure AutoDestroy;
  107.         { Read method for property SoundexValue }
  108.         function GetSoundexValue : String;
  109.         { Write method for property SoundexValue }
  110.         procedure SetSoundexValue(Value : String);
  111.         { Read method for property SoundAlike }
  112.         function GetSoundAlike : Boolean;
  113.         { Write method for property SoundAlike }
  114.         procedure SetSoundAlike(Value : Boolean);
  115.         { Read method for property SoundexPlusValue }
  116.         function GetSoundexPlusValue : String;
  117.         { Write method for property SoundexPlusValue }
  118.         procedure SetSoundexPlusValue(Value : String);
  119.         { Read method for property SoundAlikePlus }
  120.         function GetSoundAlikePlus : Boolean;
  121.         { Write method for property SoundAlikePlus }
  122.         procedure SetSoundAlikePlus(Value : Boolean);
  123.  
  124.     protected
  125.       { Protected fields of TSoundex }
  126.  
  127.       { Protected methods of TSoundex }
  128.  
  129.     public
  130.       { Public fields of TSoundex }
  131.  
  132.       { Public methods of TSoundex }
  133.         constructor Create(AOwner: TComponent); override;
  134.         destructor Destroy; override;
  135.         function Soundex(OriginalWord:string):string;
  136.         function SoundexPlus(OriginalWord:string):string;
  137.  
  138.     published
  139.       { Published properties of the component }
  140.         property FirstWord : String read FFirstWord write FFirstWord;
  141.         property SecondWord : String read FSecondWord write FSecondWord;
  142.         property SoundexValue : String
  143.              read GetSoundexValue write SetSoundexValue;
  144.         property SoundAlike : Boolean
  145.              read GetSoundAlike write SetSoundAlike
  146.              default false;
  147.         property SoundexPlusValue : String
  148.              read GetSoundexPlusValue write SetSoundexPlusValue;
  149.         property SoundAlikePlus : Boolean
  150.              read GetSoundAlikePlus write SetSoundAlikePlus;
  151.  
  152.   end;
  153.  
  154. procedure Register;
  155.  
  156. implementation
  157.  
  158. procedure Register;
  159. begin
  160.      { Register TSoundex with Indigo Widgets as its
  161.        default page on the Delphi component palette }
  162.      RegisterComponents('Indigo Widgets', [TSoundex]);
  163. end;
  164.  
  165. { Method to set variable and property values and create objects }
  166. procedure TSoundex.AutoInitialize;
  167. begin
  168.      FSoundAlike := false;
  169. end; { of AutoInitialize }
  170.  
  171. { Method to free any objects created by AutoInitialize }
  172. procedure TSoundex.AutoDestroy;
  173. begin
  174.      { No objects from AutoInitialize to free }
  175. end; { of AutoDestroy }
  176.  
  177. { Read method for property SoundexValue }
  178. function TSoundex.GetSoundexValue : String;
  179. begin
  180.    fsoundexvalue:=soundex(firstword);
  181.    getsoundexvalue:=fsoundexvalue;
  182. end;
  183.  
  184. { Write method for property SoundexValue }
  185. procedure TSoundex.SetSoundexValue(Value : String);
  186. begin
  187.      FSoundexValue := fsoundexvalue;
  188. end;
  189.  
  190. { Read method for property SoundAlike }
  191. function TSoundex.GetSoundAlike : Boolean;
  192. begin
  193.   if (Soundex(firstword)=Soundex(secondword)) then
  194.     FSoundAlike:=True
  195.   else
  196.     FSoundAlike:=False;
  197.      GetSoundAlike := FSoundAlike;
  198. end;
  199.  
  200. { Write method for property SoundAlike }
  201. procedure TSoundex.SetSoundAlike(Value : Boolean);
  202. begin
  203.      FSoundAlike := FSoundAlike;
  204. end;
  205.  
  206. { Read method for property SoundexPlusValue }
  207. function TSoundex.GetSoundexPlusValue : String;
  208. begin
  209.      fsoundexplusvalue:=soundexplus(firstword);
  210.      GetSoundexPlusValue := FSoundexPlusValue
  211. end;
  212.  
  213. { Write method for property SoundexPlusValue }
  214. procedure TSoundex.SetSoundexPlusValue(Value : String);
  215. begin
  216.      FSoundexPlusValue := FSoundexPlusValue;
  217. end;
  218.  
  219. { Read method for property SoundAlikePlus }
  220. function TSoundex.GetSoundAlikePlus : Boolean;
  221. begin
  222.   if (Soundexplus(firstword)=Soundexplus(secondword)) then
  223.     FSoundAlikeplus:=True
  224.   else
  225.     FSoundAlikeplus:=False;
  226.      GetSoundAlikePlus := FSoundAlikePlus;
  227. end;
  228.  
  229. { Write method for property SoundAlikePlus }
  230. procedure TSoundex.SetSoundAlikePlus(Value : Boolean);
  231. begin
  232.      FSoundAlikePlus := FSoundAlikePlus;
  233. end;
  234.  
  235. constructor TSoundex.Create(AOwner: TComponent);
  236. begin
  237.      inherited Create(AOwner);
  238.      AutoInitialize;
  239. end;
  240.  
  241. destructor TSoundex.Destroy;
  242. begin
  243.      AutoDestroy;
  244.      inherited Destroy;
  245. end;
  246.  
  247. function TSoundex.Soundex(OriginalWord:string):string;
  248. var
  249.   Tempstring1,Tempstring2:string;
  250.   Count:integer;
  251. begin
  252.   Tempstring1:='';
  253.   Tempstring2:='';
  254.   OriginalWord:=Uppercase(OriginalWord); {Make original word uppercase}
  255.   Appendstr(Tempstring1,OriginalWord[1]); {Use the first letter of the word}
  256.   for Count:=2 to length(OriginalWord) do
  257.       {Assign a numeric value to each letter, except the first}
  258.       case OriginalWord[Count] of
  259.         'B','F','P','V':
  260.           Appendstr(Tempstring1,'1');
  261.         'C','G','J','K','Q','S','X','Z':
  262.           Appendstr(Tempstring1,'2');
  263.         'D','T':
  264.           Appendstr(Tempstring1,'3');
  265.         'L':
  266.           Appendstr(Tempstring1,'4');
  267.         'M','N':
  268.           Appendstr(Tempstring1,'5');
  269.         'R':
  270.           Appendstr(Tempstring1,'6');
  271.         {All other letters, punctuation and numbers are ignored}
  272.       end;
  273.  
  274.   Appendstr(Tempstring2,OriginalWord[1]);
  275.  
  276.   {Go through the result, and remove any consecutive numberic values
  277.    that are duplicates}
  278.   for Count:=2 to length(Tempstring1) do
  279.     if Tempstring1[Count-1]<>Tempstring1[Count] then
  280.         Appendstr(Tempstring2,Tempstring1[Count]);
  281.  
  282.   Soundex:=Tempstring2; {This is the soundex value}
  283.  
  284. end;
  285.  
  286. function TSoundex.SoundexPlus(OriginalWord:string):string;
  287. var
  288.   Tempstring1,Tempstring2:string;
  289.   Count:integer;
  290. begin
  291.   Tempstring1:='';
  292.   Tempstring2:='';
  293.   OriginalWord:=Uppercase(OriginalWord); {Make original word uppercase}
  294.  
  295.   for Count:=1 to length(OriginalWord) do
  296.       {Assign a numeric value to each letter}
  297.       case OriginalWord[Count] of
  298.         'B','F','P','V':
  299.           Appendstr(Tempstring1,'1');
  300.         'C','G','J','K','Q','S','X','Z':
  301.           Appendstr(Tempstring1,'2');
  302.         'D','T':
  303.           Appendstr(Tempstring1,'3');
  304.         'L':
  305.           Appendstr(Tempstring1,'4');
  306.         'M','N':
  307.           Appendstr(Tempstring1,'5');
  308.         'R':
  309.           Appendstr(Tempstring1,'6');
  310.         {All other letters, punctuation and numbers are ignored}
  311.       end;
  312.  
  313.   {Go through the result, and remove any consecutive numberic values
  314.    that are duplicates}
  315.   for Count:=1 to length(Tempstring1) do
  316.     if Tempstring1[Count-1]<>Tempstring1[Count] then
  317.         Appendstr(Tempstring2,Tempstring1[Count]);
  318.  
  319.   Soundexplus:=Tempstring2; {This is the soundexplus value}
  320.  
  321. end;
  322.  
  323.  
  324.  
  325. end.
  326.